//
// Created by Administrator on 2021/12/30.
//

#ifndef CPP_LEARNING_UTILS_H
#define CPP_LEARNING_UTILS_H

#include "vector.hpp"

// 乱序 唯一化部分
template <typename T>
void myswap(T&a, T&b){
    T temp = a;
    a = b;
    b = temp;
}



/**
 * 函数对象、·
 * @tparam T
 */
template <typename T>
struct Increase{
    virtual void operator()(T& e){
        e++;
    }
};

template <typename T>
void increase(Vector<T>& V){
    V.traverse(Increase<T>());
}


// =======================================================================================================================

/**
 * 二分查找 [lo, hi)
 * 成功 返回找到元素的下表
 * 失败 返回-1
 */
typedef int Rank;
template <typename T>
Rank binSearch(T* elem, T& e,Rank lo,Rank hi){
    if(lo == hi){
        return -1;
    }

    Rank mi = (lo + hi) / 2;
    if(elem[mi] == e){
        return mi;
    }
    if(e> elem[mi]){
        return binSearch(elem,e,mi+1,hi);
    }else{
        return binSearch(elem,e,lo,mi);
    }
}


#endif //CPP_LEARNING_UTILS_H
